<?php
namespace apis;

use enums\RetCodeEnums;

/**
 * @deprecated
 * @author fzq
 * @date 2016-11-16
 */
class JuHeDataUtils implements \Phalcon\DI\InjectionAwareInterface
{
	const URL_CTRL = '/common/juhe/bankcardcheckthreeelem';
	
	static private $_instance = null;
	
	private $_di = null;
	private $_strUrl = null; 
	
	/* 
	 * @see \Phalcon\Di\InjectionAwareInterface::setDI()
	 */
	public function setDI( \Phalcon\DiInterface $dependencyInjector ) 
	{
		$this->_di = $dependencyInjector;	
		
	}

	/**
	 * 取server url
	 * @return string
	 */
	private function getServerUrl()
	{
		if( !$this->_strUrl )
		{
			$this->_strUrl = $this->_di[ 'urlCfg' ]->url->url_ct_domain . self::URL_CTRL . '/';
		}
		
		return $this->_strUrl;
	}
	
	private function __construct( $di = null )
	{
		$this->setDI( $di );
	}
	
	public static function getInstance( $di = null )
	{
		if( is_null( self::$_instance ))
		{
			self::$_instance = new JuHeDataUtils( $di );
		}	
		
		return self::$_instance;
	}
	
	/* 
	 * @see \Phalcon\Di\InjectionAwareInterface::getDI()
	 */
	public function getDI() 
	{
		return $this->_di;
	}
	
	private function getResult( $strUrl )
	{
		try
		{
			$curl = curl_init( $strUrl );
			curl_setopt( $curl, CURLOPT_TIMEOUT, 10 );
			curl_setopt( $curl, CURLOPT_HEADER, 0 );
			curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
			$strRet = curl_exec( $curl );
			curl_close( $curl );
		}
		catch ( \Exception $e )
		{
			return array( 
					'code' => RetCodeEnums::ERR_NETWORK, 
					'msg' => RetCodeEnums::ERR_NETWORK_MSG  );
		}
		
		return json_decode( $strRet );
	}
	
	/**
	 * 取指定区域ID
	 * 
	 * 用法 		
	 * $juhe = JuHeDataUtils::getInstance( $this->di );
	 * var_dump( $juhe->checkBankE3( '张小三', '银行卡号中间不能有空格', '18位' ));
	 * 
	 * @param string $strRealName
	 * @param string $strBankCard
	 * @param string $strIDCard
	 * 
	 * return false for error others for success
	 */
	public function checkBankE3( $strRealName, $strBankCard, $strIDCard )
	{
		if( !$strRealName || trim( $strRealName ) == '' ||
			!$strBankCard || trim( $strBankCard ) == '' || 
			!$strIDCard || trim( $strIDCard ) == '' )
		{
			return false;
		}
		
		$strUrl = $this->getServerUrl() . '/realname/' . $strRealName . '/idcard/' . $strIDCard . '/bankcard/' . $strBankCard;
		
		return $this->getResult( $strUrl );
	}
	
	
}